package com.platform.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.annotation.SysLog;
import com.platform.entity.FarmInfoEntity;
import com.platform.entity.UserVo;
import com.platform.oss.OSSFactory;
import com.platform.service.FarmInfoService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.DateUtils;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.ResourceUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 农机手册Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2020-02-23 21:56:20
 */
@Api(tags = "农机手册管理")
@RestController
@RequestMapping("api/farminfo")
public class FarmInfoController extends ApiBaseAction{
    @Autowired
    private FarmInfoService farmInfoService;
    
    /**
     * 查询一级目录
     */
    @ApiOperation(value = "查询农机手册一级目录全部信息")
    //@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "农机手册id", paramType = "path", required = true)})
    @IgnoreAuth
    @GetMapping(value = "firstLevel")
    public Object firstLevel(@LoginUser UserVo loginUser) {
    	
    	List<FarmInfoEntity> list=farmInfoService.slectFirstLevel();
		return toResponsSuccess(list);
        
    }
    
    /**
     * 根据左右值，查找当前节点的所有子节点（有层级时查询只查当前节点下一级目录的子节点）
     */
    @ApiOperation(value="查询此目录下的所有子目录")
    @ApiImplicitParams({@ApiImplicitParam(name="lef",value="左值",paramType="query",required=true,dataType="Integer"),
    					@ApiImplicitParam(name="rgt",value="右值",paramType="query",required=true,dataType="Integer"),
    					@ApiImplicitParam(name="level",value="层级",paramType="query",required=true,dataType="Integer"),
    					@ApiImplicitParam(name="page",value="页数",paramType="query",required=true,dataType="Integer"),
    					@ApiImplicitParam(name="size",value="单页数量",paramType="query",required=true,dataType="Integer")})
    @IgnoreAuth
    @GetMapping(value = "findChildNodes")
    public Object findChildNodes(@RequestParam Integer lef,
    							@RequestParam Integer rgt,
    							@RequestParam Integer level,
    							@RequestParam(defaultValue="0") Integer page,
    							@RequestParam(defaultValue="10")Integer size) {
    	Map<String, Integer> map=new HashMap<String,Integer>();
    	if(level!=null && !level.equals("")) {
    		map.put("level", level);
    	}
    	map.put("lef", lef);
    	map.put("rgt", rgt);
    	map.put("page", page);
    	map.put("size", size);
    	List<FarmInfoEntity> list=farmInfoService.selectChildNodes(map);
    	return toResponsSuccess(list);
    }
    /**
     * 模糊查询
     */
    @ApiOperation(value = "模糊查询")
    @ApiImplicitParams({@ApiImplicitParam(name="keyword",value="关键字",paramType="query",required=true,dataType="Strng"),
    					@ApiImplicitParam(name="root",value="根目录",paramType="query",required=true,dataType="String"),
    					@ApiImplicitParam(name="page",value="页数",paramType="query",required=true,dataType="Integer"),
    					@ApiImplicitParam(name="size",value="单页数量",paramType="query",required=true,dataType="Integer")})
    @IgnoreAuth
    @GetMapping(value="search")
    public Object searchFuzzy(@RequestParam String keyword,@RequestParam String root,
    		@RequestParam(defaultValue="0") Integer page,
			@RequestParam(defaultValue="10")Integer size) {
    	//防止传过来的keyword为空，进行全表查询
    	List<FarmInfoEntity> list=null;
    	Map map = new HashMap();
    	if (root.equals("全部机型")) {
			root=null;
		}
    	if(keyword!=null && !keyword.equals("")) {
    		map.put("keyword", keyword);
    		map.put("root", root);
    		map.put("page", page);
        	map.put("size", size);
    		list=farmInfoService.searchFuzzy(map);	
    	}
    	return toResponsSuccess(list);
	}
    /**
     * 模糊查询，获取总条数
     */
    @ApiOperation(value = "模糊查询，获取总条数")
    @ApiImplicitParams({@ApiImplicitParam(name="keyword",value="关键字",paramType="query",required=true,dataType="Strng"),
    					@ApiImplicitParam(name="root",value="根目录",paramType="query",required=true,dataType="String")})
    @IgnoreAuth
    @GetMapping(value="searchCount")
    public Object searchFuzzyCount(@RequestParam String keyword,@RequestParam String root) {
    	//防止传过来的keyword为空，进行全表查询
    	int count=0;
    	Map map = new HashMap();
    	if (root.equals("全部机型")) {
			root=null;
		}
    	if(keyword!=null && !keyword.equals("")) {
    		map.put("keyword", keyword);
    		map.put("root", root);
    		count=farmInfoService.searchFuzzyCount(map);	
    	}
    	return toResponsSuccess(count);
	}
    
    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("farminfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<FarmInfoEntity> farmInfoList = farmInfoService.queryList(query);
        int total = farmInfoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(farmInfoList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("farminfo:info")
    @IgnoreAuth
    public R info(@PathVariable("id") Integer id) {
    	System.out.println(id);
        FarmInfoEntity farmInfo = farmInfoService.queryObject(id);

        return R.ok().put("farmInfo", farmInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @IgnoreAuth
    //@RequiresPermissions("farminfo:save")
    public R save(@RequestBody FarmInfoEntity farmInfo) {
    	System.out.println("-----------------"+farmInfo.toString());
    	farmInfo.setAllName(farmInfo.getName());
        farmInfoService.save(farmInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @IgnoreAuth
    //@RequiresPermissions("farminfo:update")
    public R update(@RequestBody FarmInfoEntity farmInfo) {
        farmInfoService.update(farmInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除菜单")
    @RequestMapping("/delete")
    //@RequiresPermissions("farminfo:delete")
    @IgnoreAuth
    public R delete(@RequestBody Integer[] ids) {
    	System.out.println("----------------------------"+ids[0]);
    	if(ids.length>1) {
    		return R.error("抱歉不支持批量删除！");
    	}
        farmInfoService.deleteBatch(ids);

        return R.ok();
    }
    
    /**
     * 选择菜单（增加、修改菜单）
     */
    @RequestMapping("/select")
    @IgnoreAuth
    //@RequiresPermissions("farminfo:select")
    public R select() {
    	//查询列表菜单
    	List<FarmInfoEntity> list=farmInfoService.queryNotPdfList();
    	
    	//添加顶级菜单
    	FarmInfoEntity root=new FarmInfoEntity();
    	root.setId(0);
    	root.setName("一级菜单");
    	root.setParentId(-1L);
    	root.setOpen(true);
    	list.add(root);
    	return R.ok().put("menuList", list);
    }

    

	/**
     * 查看所有列表
     */
    @IgnoreAuth
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<FarmInfoEntity> list = farmInfoService.queryList(params);
        return R.ok().put("list", list);
    }
    

}
